package com.smart.campus.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import com.smart.campus.dto.ShStudent;
import com.smart.campus.pojo.PagedResponse;
import com.smart.campus.pojo.ResponseResult;
import com.smart.campus.service.ShStudentServiceFeign;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.List;

@Api(tags = "学生管理")
@Slf4j
@RestController
@RequestMapping(value = "shStudent")
public class ShStudentController {
    @Autowired
    private ShStudentServiceFeign shStudentServiceFeign;

    @RequestMapping(value = "pageShStudent")
    public ResponseResult<PagedResponse<ShStudent>> pageShStudent(@RequestBody ShStudent shStudent, @RequestParam(value = "pageNum")Integer pageNum,@RequestParam(value = "pageSize")Integer pageSize){
        return new ResponseResult<>(200,"成功",shStudentServiceFeign.pageShStudent(shStudent,pageNum,pageSize));
    }

    @RequestMapping(value = "addShStudent")
    public ResponseResult addShStudent(@RequestBody ShStudent shStudent){
        try {
            shStudentServiceFeign.addShStudent(shStudent);
            return new ResponseResult<>(200,"成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseResult<>(444,"失败");
        }
    }
    @RequestMapping(value = "delShStudent")
    public ResponseResult delShStudent(@RequestParam(value = "id")Integer id){
        try {
            shStudentServiceFeign.delShStudent(id);
            return new ResponseResult<>(200,"成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseResult<>(444,"失败");
        }
    }
    @RequestMapping(value = "findShStudentById")
    public ResponseResult<ShStudent> findShStudentById(@RequestParam(value = "id")Integer id){
        return new ResponseResult<>(200,"成功",shStudentServiceFeign.findShStudentById(id));
    }
    @RequestMapping(value = "updateShStudent")
    public ResponseResult updateShStudent(@RequestBody ShStudent shStudent){
        try {
            shStudentServiceFeign.updateDateShStudent(shStudent);
            return new ResponseResult<>(200,"成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseResult<>(444,"失败");
        }
    }

    /**
     * 导出
     * @param shStudent
     * @param response
     */
    @PostMapping("/exportUsersExcel")
    public void exportExcel(@RequestBody ShStudent shStudent, HttpServletResponse response){
        // 查询用户信息集合
        List<ShStudent> userList = shStudentServiceFeign.findShStudentList(shStudent);

        //.xlsx格式
        //response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        //.xls格式
        response.setContentType("application/vnd.ms-excel;charset=utf-8");
        response.setCharacterEncoding("utf-8");
        //前端存在跨域不成功，设置可访问
        response.setHeader("Access-Control-Allow-Origin","*");
        //设置不要缓存
        response.setHeader("Pragma", "No-cache");
        try {
            // 这里URLEncoder.encode可以防止中文乱码
            String fileName = URLEncoder.encode("shStudentList", String.valueOf(StandardCharsets.UTF_8));
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xls");
            //设置sheet名
            ExportParams params = new ExportParams();
            params.setSheetName("学生信息表");
            //设置标题
            params.setTitle("学生信息");
            // 这里需要设置不关闭流
            Workbook workbook = ExcelExportUtil.exportExcel(params, ShStudent.class, userList);
            //输出流
            OutputStream outStream = response.getOutputStream();
            //浏览器下载
            workbook.write(outStream);
            //关闭流
            outStream.flush();
            outStream.close();
        } catch (IOException e) {
            response.reset();
            response.setContentType("application/json");
            response.setCharacterEncoding("utf-8");
            System.out.println(e.getMessage());
        }
    }

    /**
     * 导入
     * @param file
     * @return
     */
    @PostMapping(value = "importExcel")
    public ResponseResult importExcel(@RequestPart("file") MultipartFile file) {
        try {
            shStudentServiceFeign.importExcel(file);
            return new ResponseResult(200, "导入信息成功！");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseResult(444, "导入信息失败！");
        }
    }
}
